package gui.other;

import j_util.Utiles;
import model.eval.Test;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*

    This file is part of Logica-Logike.

    Logica-Logike is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation version 3 of the License.

    Logica-Logike is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Logica-Logike.  If not, see <http://www.gnu.org/licenses/>.

 */
/**
 * @author Jose Martin Rozanec;
 */
public class TestDialog extends JDialog {
    private JFileChooser chooser;
    private JTextArea previewArea;
    private JScrollPane previewTestScrollPane;

    public TestDialog(JFileChooser chooser){
        this.chooser=chooser;
        initGUI();
    }

    private void initGUI(){
        previewArea=new JTextArea(10,20);
        previewTestScrollPane = new JScrollPane(previewArea);
        previewTestScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        previewTestScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        previewTestScrollPane.setPreferredSize(new Dimension(100,450));
        previewTestScrollPane.setMaximumSize(new Dimension(100,450));
        previewTestScrollPane.setMinimumSize(new Dimension(100,450));
        previewArea.setEditable(false);
        chooser.setBackground(Color.WHITE);
        chooser.addPropertyChangeListener(new PropertyChangeListener(){
            public void propertyChange(PropertyChangeEvent evt) {
                Test test;
                String previewString=null;
                RandomAccessFile raf;
                if(chooser.getSelectedFile()!=null){
                    String extension = Utiles.getExtension(chooser.getSelectedFile());
                    if(extension.equalsIgnoreCase("xml")){
                        try {
                            raf = new RandomAccessFile(chooser.getSelectedFile(),"r");
                            previewString= raf.readUTF();
                            previewString=Utiles.formatXML(previewString);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else{
                        if(extension.equalsIgnoreCase("tst")){
                            test= Utiles.deserialize(chooser.getSelectedFile().getPath());
                            previewString=test.toString();
                        }
                    }
                    if(previewString!=null){
                        previewArea.setText(previewString);
                    }
                }
            }
        });
        add(chooser,BorderLayout.WEST);
        add(previewTestScrollPane,BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        TestDialog dialog = new TestDialog(new JFileChooser());
        dialog.setPreferredSize(new Dimension(750,450));        
        dialog.pack();
        dialog.setVisible(true);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    }
}
